package com.naiterui.ehp.bs.patient.constants;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.naiterui.ehp.bp.utils.event.EventUtil;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.patient.handler.data.BizConfigEvent;
import com.naiterui.ehp.bs.wechat.utils.WeChatApiUtil;

import lombok.Getter;

/**
 * @author gaoliangliang
 * @date 2019-07-09 17:57
 * @since 1.0.0
 */
@Getter
public enum PatientJumperEnum {

    /**
     * 首页
     */
    FRONT_PAGE("frontPage", true, null, "vue/dist/#/myDoctor"),

    /**
     * 医生列表
     */
    DOCTOR_LIST("doctorList", false, "doctortype", "work/html/my-doctor.html#myDoctor,"),

    /**
     * 医生详情
     */
    DOCTOR_DETAIL("doctorDetail", false, "doctorId", "work/html/docDetails.html#docDetails,"),

    /**
     * 小程序医生详情
     */
    MINI_DOCTOR_DETAIL("miniDoctorDetail", true, "doctorId", "/pages/famousDoctorDetail/famousDoctorDetail?type=1&doctorId="),

    /**
     * 小程序消息历史
     */
    MINI_MESSAGE_HISTORY("miniMessageHistory", true, "doctorId", "/pages/consult/chat/chat?doctorId="),

    /**
     * 处方详情
     */
    RECOM_DETAIL("recomDetail", false, "recommendId", "work/html/prescription-detail.html#prescription_detail,"),

    /**
     * 咨询记录
     */
    CHAT_HISTORY("chatHistory", false, "toId", "work/html/chatDetail.html#chatDetail,"),

    /**
     * 购物车列表
     */
    CART_LIST("cartList", false, null, "work/html/shopcart.html#shopcart,"),

    /**
     * 订单列表
     */
    ORDER_LIST("orderList", false, null, "work/html/order.html#my_order,"),

    /**
     * 地址列表
     */
    ADDRESS_LIST("addressList", false, "userAddress", "work/html/settlementNew.html#shipping_address,"),

    /**
     * 个人资料
     */
    PATIENT_INFO("patientInfo", false, null, "work/html/patient-person-info.html#person_info,"),

    /**
     * 实名认证
     */
    PATIENT_AUTH("patientAuth", true, null, "vue/dist/#/authname?auth=1"),

    /**
     * 随访提醒
     */
    REVISIT_LIST("revisitList", true, "doctorId", "vue/dist/#/prescribe_list/"),

    /**
     * 用药提醒
     */
    DRUG_REMIND("drugRemind", true, "recomId", "#/medicationAlarm/"),

    /**
     * 咨询付费详情
     */
    PAY_CONSULT("payConsult", false, "doctorId", "work/html/consultation.html#consultation,"),

    /**
     * 医生填写病历详情
     */
    MEDICAL_RECORD_DETAIL("medicalRecordDetail", true, null, "vue/dist/#/recordDetails/"),

    /**
     * 患者病历选择
     */
    PATIENT_MEDICAL_RECORD_LIST("medicalRecordList", true, "doctorId", "vue/dist/#/jiuzhenList/change?doctorId="),

    /**
     *
     */
    PATIENT_MEDICAL_DISEASE("medicalDisease", true, "doctorId", "/vue/dist/#/medicalRecord/"),

    /**
     * 在线医疗免责协议详情
     */
    DISCLAIMER_DETAIL("disclaimerDetail", true, "doctorId", "vue/dist/#/serviceAgreement/"),

    /**
     * 专家名医列表
     */
    CLINIC_LIST("clinicList", true, null, "vue/dist/#/doctorList"),

    /**
     * 会话投诉
     */
    CONSULT_COMPLAINT("consultComplaint", true, "doctorId", "vue/dist/#/complaint/"),

    /**
     * 会话评价
     */
    CONSULT_COMMENT("consultComment", true, "doctorId", "vue/dist/#/comment/"),

    /**
     * 处方列表
     */
    PRESCRIPTION_LIST("prescriptionList", true, null, "vue/dist/#/prescriptionList"),
	
	/**
     * 就诊人列表
     */
    PATIENT_INQUIRER_LIST("patientInquirerList", true, null, "/vue/dist/#/jiuzhenList/base"),


    /**
     * 院务公开
     */
    NOTICE_LIST("noticeList", true, null, "vue/dist/#/noticeList/"),

    SYSTEM("system", false, "param", "work/html/reload404.html"),

    EMPTY("empty", false, null, "work/html/reload404.html"),

    /**
     * 首页
     */
    HOME_PAGE("homePage", true, null, "vue/dist/#/myDoctor"),
    ;

    private String scene;
    private boolean vueFlag;
    private String param;
    private String uri;

    public String encodeCodeUrl(String paramValue, String code) {
        String result = encodeUrl(paramValue);
        return result.replaceAll("#", "?code=" + code + "#");
    }

    public String encodeUrl(String paramValue) {

        if (SYSTEM.getScene().equals(scene) && StringUtils.isNumeric(paramValue)) {
            EventUtil.publishEvent(new BizConfigEvent(scene, paramValue, Integer.parseInt(paramValue)));
            return ConfigUtil.getString("ehp.domain.patientH5") + "/" + uri;
        }

        //paramValue = StringUtils.isEmpty(paramValue) ? "" : paramValue;
        String paramStr = StringUtils.defaultString(paramValue);
        //H5 vue框架参数使用restful风格，否则使用json数组切+urlEncode

        if (!vueFlag) {
            Map<String, String> paramMap = new HashMap<String, String>() {
                private static final long serialVersionUID = 5445742798526230403L;

                {
                if (param != null) {
                    put(param, StringUtils.defaultString(paramValue));
                    if (PAY_CONSULT.getScene().equals(scene)){
                        put("type", "1");
                    }
                }
            }};
            paramStr = new JSONArray(Collections.singletonList(paramMap)).toJSONString();
        }
        if (MINI_DOCTOR_DETAIL.getScene().equals(scene) || MINI_MESSAGE_HISTORY.getScene().equals(scene)) {
            return uri + paramStr;
        } else {
            return ConfigUtil.getString("ehp.domain.patientH5") + "/" + uri + paramStr;
        }
    }

    /*public static void main(String[] args) {
        String str = PatientJumperEnum.MINI_MESSAGE_HISTORY.encodeCodeUrl("129", "sdfsfsd");
        System.out.println(str);
    }*/

    public String encodeWxParamStr(String... paramValues) {
        String paramValue = paramValues != null && paramValues.length > 0 ? paramValues[0] : null;
        try {
            String paramStr = paramValue == null ? "" : WeChatApiUtil.WX_DEFAULT_PARAM_SPLIT + paramValue;
            return URLEncoder.encode(scene + paramStr, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return scene + WeChatApiUtil.WX_DEFAULT_PARAM_SPLIT + paramValue;
        }
    }

    PatientJumperEnum(String scene, boolean vueFlag, String param, String uri) {
        this.scene = scene;
        this.vueFlag = vueFlag;
        this.param = param;
        this.uri = uri;
    }

    public static PatientJumperEnum getByScene(String scene) {
        for (PatientJumperEnum jumperEnum : PatientJumperEnum.values()) {
            if (jumperEnum.getScene().equals(scene)) {
                return jumperEnum;
            }
        }
        return EMPTY;
    }

}